﻿using AppDataStruct;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Image = System.Drawing.Image;

namespace YSLPages
{
    public partial class PagePreviewChecking : PageControlBase
    {
    
        private SFZInfo _sfz = new SFZInfo();

        private PreApplyInfo _appInfo = null;

        private TextBox textBoxYJDZ = null;

        private TextBox textBoxDocumentType = null;

        private PictureBox pictureBoxSignature = null;

        private Label labelSignature = null;

        private PictureBox pictureBoxLivePhoto = null;

        private bool HZChecked = false;

        private bool SCZChecked = false;

        private bool DLZChecked = false;

        public PagePreviewChecking()
        {            
            InitializeComponent();
            SetProcessBase(ProcessPreviewChecking.Instance);
        }

        private void GetControls()
        {
            textBoxYJDZ = GetControl<TextBox>("textBoxYJDZ") as TextBox;
            labelSignature = GetControl<Label>("labelSignature") as Label;
            textBoxDocumentType = GetControl<TextBox>("textBoxDocumentType") as TextBox;
            pictureBoxSignature = GetControl<PictureBox>("pictureBoxSignature") as PictureBox;
            pictureBoxLivePhoto = GetControl<PictureBox>("pictureBoxLivePhoto") as PictureBox;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();        
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            _sfz = Process.GetValue<SFZInfo>("SFZInfo");
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");         
            if (Process.ContainsKey("HZChecked"))
            {
                HZChecked = Process.GetValue<bool>("HZChecked");
            }
            else
            {
                HZChecked = false;
            }
            if (Process.ContainsKey("DLZChecked"))
            {
                DLZChecked = Process.GetValue<bool>("DLZChecked");
            }
            else
            {
                DLZChecked = false;
            }
            if (Process.ContainsKey("SCZChecked"))
            {
                SCZChecked = Process.GetValue<bool>("SCZChecked");
            }
            else
            {
                SCZChecked = false;
            }
            InitTextBoxs();
            InitPictureBox();
            Speak("请确认申请信息，确认无误后点击下一步");
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            HZChecked = false;
            SCZChecked = false;
            DLZChecked = false;
            if (pictureBoxSignature.Image != null)
            {
                pictureBoxSignature.Image.Dispose();
                pictureBoxSignature.Image = null;
            }

            if (pictureBoxLivePhoto!=null && pictureBoxLivePhoto.Image != null)
            {
                pictureBoxLivePhoto.Image.Dispose();
                pictureBoxLivePhoto.Image = null;
            }
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        private void InitPictureBox()
        {
            if(this.InvokeRequired)
            {
                Action handler = new Action(InitPictureBox);
                this.Invoke(handler);
                return;
            }
            if (_config.WorkFlow.Signature && File.Exists(_appInfo.HZSQ.SXQM))
            {
                labelSignature.Visible = true;
                try
                {
                    pictureBoxSignature.SizeMode = PictureBoxSizeMode.Zoom;
                    Image image = Image.FromFile(_appInfo.HZSQ.SXQM);
                    pictureBoxSignature.Image = image;
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex.ToString());
                }            
            }
            else
            {
                labelSignature.Visible = false;
                pictureBoxSignature.Image = null;
            }

            if (pictureBoxLivePhoto!=null && File.Exists(_appInfo.XCRX))
            {
                try
                {
                    pictureBoxLivePhoto.SizeMode = PictureBoxSizeMode.Zoom;
                    Image image = Image.FromFile(_appInfo.XCRX);
                    pictureBoxLivePhoto.Image = image;
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex.ToString());
                }
            }
        }

        protected void InitTextBoxs()
        {
            if(this.InvokeRequired)
            {
                Action handler = new Action(InitTextBoxs);
                this.Invoke(handler);
                return;
            }
            if (_appInfo == null || _sfz == null)
            {
                return;
            }
            string docType = "";
            if (HZChecked)
            {
                docType = "护照 ";
            }
            if (SCZChecked)
            {
                docType += "港澳通行证 ";
            }
            if (DLZChecked)
            {
                docType += "台湾通行证";
            }
            textBoxDocumentType.Text = docType;
           
            Type type = _appInfo.GetType();
            string FieldName = "";
            foreach (TextBox tb in TextBoxs)
            {
                if(tb == textBoxDocumentType)
                {
                    continue;
                }
                Log.WriteAsync("TextBoxName:" + tb.Name);
                if (!TextBoxBindings.ContainsKey(tb.Name))
                {
                    continue;
                }
                FieldName = TextBoxBindings[tb.Name].Key;
                Log.WriteAsync("TextBox:" + TextBoxBindings[tb.Name].Key);
                System.Reflection.PropertyInfo propertyInfo = type.GetProperty(FieldName);
                if (propertyInfo != null)
                {
                    if (propertyInfo.PropertyType == typeof(CodeMap))
                    {
                        CodeMap cm = (CodeMap)propertyInfo.GetValue(_appInfo, null);
                        tb.Text = cm.Value; //给对应属性赋值
                    }
                    else
                    {
                        tb.Text = (string)propertyInfo.GetValue(_appInfo, null); //给对应属性赋值
                    }
                    Log.WriteAsync("文本框" + tb.Name + "：" + tb.Text);
                }
                else
                {
                    Log.WriteAsync("找不到名称为[" + FieldName + "]属性.");
                }
            }
        }

        public override void StartWork()
        {
            base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            UpdateData();
            ClearData();
            Process.ToNext();
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            StopCountdown();
            if (UserAction == ComLib.ComType.UserApplyAction.TakePhotoAndWeiYinTB)
            {
                return;
            }
            if (UserAction == ComLib.ComType.UserApplyAction.TakePhotoAndYSL)
            {
                return;
            }
            //base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnCancel_Click(sender, e);
        }
    }
}
